package com.jiazhong.集合类.Collection.Set.HashSet;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo {
    public static void main(String[] args) {
        //创建HashSet集合对象
        HashSet<String> hashSet = new HashSet<>();
        //向集合中添加元素
        hashSet.add("aaa");
        hashSet.add("bbb");
        hashSet.add("ccc");

        hashSet.add("ccc");//不能存放重复数据
        System.out.println(hashSet);

        //获取集合中的一个元素
        //遍历集合中元素
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {//检测集合中是否还有元素
            String str = iterator.next();//取出元素
            System.out.println(str);
        }
        System.out.println("---------");

        for (String str1 : hashSet) {
            System.out.println(str1);
        }

        hashSet.forEach(System.out::println);

        HashSet<User> userHashSet=new HashSet<>();
        User user1=new User(1,"admin",123456);
        User user2=new User(2,"baton",111111);
        User user3=new User(3,"bush",222222);
        User user4=new User(4,"lucky",333333);
        userHashSet.add(user1);
        userHashSet.add(user2);
        userHashSet.add(user3);
        userHashSet.add(user4);
        for (User user:userHashSet
             ) {
            System.out.println(user);
        }
    }
}
